<template>
  <div class="scrollbar">
    <el-scrollbar>
      <el-form 
        label-position="right" 
        :model="selectTable"
        ref="addForm" 
        label-width="100px" 
        :inline="false"
        autocomplete="off"
        >
        
        <el-row>
          <el-col v-if="this.id">
            <el-form-item label="复制网址">
              <el-input v-model="selectTable.manuscriptPath" maxLength="100" style="width: 70%;" disabled></el-input>
              <el-button icon="el-icon-tickets" v-clipboard:copy='selectTable.manuscriptPath' v-clipboard:success='clipboardSuccess'>复制</el-button>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="名称" prop="headline" :rules="[rule[0]]">
              <el-input placeholder="请输入名称" v-model="selectTable.headline" maxLength="100"></el-input>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="状态" prop="status" :rules ="[rule[0]]">
              <el-switch
                v-model="selectTable.status"
                :active-value="0"
                active-text="启用"
                :inactive-value="1"
                inactive-text="停用"
                >
              </el-switch>
            </el-form-item>
          </el-col>
          
          <el-col>
            <el-form-item label="PC模板" prop="templetId">
              <tplselect v-model="selectTable.templetId"></tplselect>
            </el-form-item>
          </el-col>
          
          <el-col>
            <el-form-item label="WAP模板" prop="mTempletId">
              <tplselect v-model="selectTable.mTempletId"></tplselect>
            </el-form-item>
          </el-col>
        
          <el-col>
            <el-form-item label="内容" prop="detail" :rules ="[rule[0]]">
              <editor v-model="selectTable.detail"
                v-if="!pload"
                :config="{initialFrameHeight:120}">
              </editor>
            </el-form-item>
          </el-col>
        
          <el-col>
            <el-form-item label="摘要" prop="summary" :rules="[rule[0]]">
              <el-input type="textarea" placeholder="请输入摘要" v-model="selectTable.summary" maxLength="200"></el-input>
            </el-form-item>
          </el-col>
        
          <el-col>
            <el-form-item label="备注" prop="comment">
              <el-input placeholder="请输入摘要" v-model="selectTable.comment" maxLength="200"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-scrollbar>
    <div v-loading="pload" class="footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.scrollbar {
  height: 100%;
  overflow: hidden;
  padding-bottom: 50px;
  position: relative;
  .el-scrollbar {
    height: 100%;
  }
  .el-form {
    padding: 20px;
  }
  .title {
    text-align: center;
    font-size: 16px;
    border-bottom: 1px #e4e4e4 solid;
    padding-bottom: 15px;
    margin-bottom: 15px;
  }
  .footer {
    text-align: center;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 50px;
  }
}
</style>
<script>
import { pageSave, pageModify, pageGetById } from '@/utils/api'
import tplselect from '../sysTpl/tplSelect'
import clipboard from '@/directive/clipboard/index.js' // use clipboard by v-directive
export default {
  directives: {
    clipboard
  },
  components: {
    editor: () => import('@/components/Ueditor'),
    tplselect
  },
  props: {
    id: Number
  },
  created() {
    if (this.id) {
      this.getData()
    } else {
      this.pload = false
    }
  },
  watch: {
    show(newVal) {
      this.$emit('close')
    }
  },
  computed: {
    title() {
      if (this.id) {
        return '修改'
      } else {
        return '添加'
      }
    }
  },
  data() {
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    clipboardSuccess() {
      this.$message({
        message: '复制成功',
        type: 'success',
        duration: 1500
      })
    },
    async submitForm(formName) {
      await this.$refs[formName].validate()
      try {
        this.pload = true
        if (this.id) {
          await pageModify(this.selectTable)
        } else {
          await pageSave(this.selectTable)
        }
        this.$notify({ title: '操作成功', type: 'success', duration: 5000 })
        this.$emit('close')
        this.$emit('getData')
      } catch (e) {
        this.pload = false
      }
    },
    async getData() {
      this.pload = true
      const Datas = await pageGetById({
        leafletId: this.id
      })
      this.selectTable = Datas
      this.pload = false
    }
  }
}
</script>